package top.dbpanda.cmsjpa.learn.thread.juc;

import lombok.AllArgsConstructor;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 20018954
 */
public class TestCyclicBarrier {
    @AllArgsConstructor
    static class MyThread implements Runnable {
        //用于控制开会开始的屏障
        private CyclicBarrier barrier;
        //开会人员
        private String name;

        @Override
        public void run() {
            try {
                Thread.sleep((int) (10000 * Math.random()));
                System.out.println(this.name + "已经到会");
                barrier.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                throw new RuntimeException(e);
            }
            System.out.println(this.name + "会议开始");
        }
    }

    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        executorService.submit(new MyThread(cyclicBarrier,"zs"));
        executorService.submit(new MyThread(cyclicBarrier,"ls"));
        executorService.submit(new MyThread(cyclicBarrier,"ww"));
        executorService.shutdown();
    }
}
